using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Media;

namespace TestGame {
    /// <summary>
    /// The main menu screen is the first thing displayed when the game starts up.
    /// </summary>
    class MainMenuScreen : MenuScreen {
        #region Fields

        ContentManager content;

        #endregion

        #region Constructor

        /// <summary>
        /// Constructor fills in the menu contents.
        /// </summary>
        public MainMenuScreen() : base("Main Menu") {
            // Create our menu entries.
            MenuEntry singleplayerEntry = new MenuEntry("Single Player");
            MenuEntry multiplayerEntry = new MenuEntry("Multiplayer");
            MenuEntry optionsMenuEntry = new MenuEntry("Options");
            MenuEntry exitMenuEntry = new MenuEntry("Exit");

            // Hook up menu event handlers.
            singleplayerEntry.Selected += SinglePlayerEntrySelected;
            multiplayerEntry.Selected += MultiPlayerEntrySelected;
            optionsMenuEntry.Selected += OptionsMenuEntrySelected;
            exitMenuEntry.Selected += QuitMenuEntrySelected;

            // Add entries to the menu.
            MenuEntries.Add(singleplayerEntry);
            MenuEntries.Add(multiplayerEntry);
            MenuEntries.Add(optionsMenuEntry);
            MenuEntries.Add(exitMenuEntry);
        }

        #endregion

        #region Content Management

        /// <summary>
        /// Loads the content involved with the MainMenu screen.
        /// </summary>
        public override void LoadContent() {
 	        base.LoadContent();

            if (content == null) {
                content = new ContentManager(ScreenManager.Game.Services, "Content");
            }

            Song mainMenuSong = content.Load<Song>("music/Title");
            MediaPlayer.Play(mainMenuSong);
        }

        /// <summary>
        /// Unloads all content for the Screen and stops any Songs playing.
        /// </summary>
        public override void UnloadContent() {
            base.UnloadContent();
            MediaPlayer.Stop();
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Event handler for when the Singe Player menu entry is selected.
        /// </summary>
        void SinglePlayerEntrySelected(object sender, PlayerIndexEventArgs e) {
            LoadingScreen.Load(ScreenManager, true, e.PlayerIndex, new SinglePlayerScreen());
        }

        /// <summary>
        /// Event handler for when the Multiplayer menu entry is selected.
        /// </summary>
        void MultiPlayerEntrySelected(object sender, PlayerIndexEventArgs e) {
            ScreenManager.AddScreen(new MultiplayerMenuScreen(), e.PlayerIndex);
        }

        /// <summary>
        /// Event handler for when the Options menu entry is selected.
        /// </summary>
        void OptionsMenuEntrySelected(object sender, PlayerIndexEventArgs e) {
            ScreenManager.AddScreen(new OptionsMenuScreen(), e.PlayerIndex);
        }


        /// <summary>
        /// When the user cancels the main menu, ask if they want to exit the sample.
        /// </summary>
        void QuitMenuEntrySelected(object sender, PlayerIndexEventArgs e) {
            ScreenManager.Game.Exit();
        }

        /// <summary>
        /// Event handler for when the user selects ok on the "are you sure
        /// you want to exit" message box.
        /// </summary>
        void ConfirmExitMessageBoxAccepted(object sender, PlayerIndexEventArgs e) {
            ScreenManager.Game.Exit();
        }

        #endregion
    }
}
